package weixin.core.controller;

import com.github.sd4324530.fastweixin.api.JsAPI;
import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.response.GetSignatureResponse;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.api.response.OauthGetTokenResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lxb on 2015/12/22.
 */
@Controller
@RequestMapping("sdktest")
public class SDKController {
    private static final Logger LOG  = LoggerFactory.getLogger(SDKController.class);
    @Autowired
    private JsAPI jsAPI;
    @Value("${appid}")
    private String appid;
    @Value("${token}")
    private String token;
    @Autowired
    private OauthAPI oauthAPI;
    @RequestMapping("jssdk")
    public String toSdktest(HttpServletRequest request) {
        this.setAttribute("http://bb15662683581.vicp.cc/sdktest/jssdk", request);
        String code=request.getParameter("code");
        if(code !=null) {
        OauthGetTokenResponse response=oauthAPI.getToken(code);
        GetUserInfoResponse getUserInfoResponse=oauthAPI.getUserInfo(token,response.getOpenid());
        }

        return "jssdk";
    }

    public void setAttribute(String url, HttpServletRequest request) {
        GetSignatureResponse getSignatureResponse=jsAPI.getSignature(url);
        request.setAttribute("signature", "'" + getSignatureResponse.getSignature() + "'");
        request.setAttribute("DateTime", "'" + getSignatureResponse.getTimestamp() + "'");
        request.setAttribute("appid", "'" + appid + "'");
        request.setAttribute("nonceStr", "'" + getSignatureResponse.getNoncestr() + "'");
    }

}
